<?php
/**
 * Created by PhpStorm.
 * User: chenzhitao
 * Date: 2018/3/13
 * Time: 下午1:34
 */

namespace App\Busi\Traits;


use App\Models\Company;
use App\Models\Level;
use App\Models\Quota;
use App\Models\SecQualifyCategory;
use App\Models\SupQualifyCategory;

Trait CategoryTrait
{

    public function getSupCateNameByID($id){
        $where = [
            ['id','=',$id]
        ];
        $fields = ['id','category_name'];
        $data = SupQualifyCategory::where($where)->select($fields)->first();
        $data = $data->toArray();
        if($data){
            return "<a class='success' href='supqualifycate/".$data['id']."' target='_blank'>{$data['category_name']}</a><br>";
        }else{
            return "";
        }

    }
    public function getSupCateIdById($id){
        $where = [
            ['id','=',$id]
        ];
        $fields = ['parent_cate_id'];
        $data = SecQualifyCategory::where($where)->select($fields)->first();
        if($data){
           return $data['parent_cate_id'];
        }else{
            return "";
        }

    }

    public function getSubCateNameByID($id){
        $where = [
            ['id','=',$id]
        ];
        $fields = ['id','category_name'];
        $data = SecQualifyCategory::where($where)->select($fields)->first();
        $data = $data->toArray();
        if($data){
            return "<a class='success' href='subqualifycate/".$data['id']."' target='_blank'>{$data['category_name']}</a><br>";
        }else{
            return "";
        }

    }

    /**
     * 获取顶级分类,以及子分类
     */
    public function getSupCategoryListByType($type){
        $where = [
            ['type',"=",$type],
            ['flag','=',1]
        ];
        $fileds = ["id","category_name"];
        $data = SupQualifyCategory::where($where)->select($fileds)->orderBy("show_order","asc")->get();

        foreach ($data as &$item){
            $item["subcategoy"] = $this->getSubCategoryByPid($item["id"]);

        }
        return $data;

    }

    /**
     * 根据父级id获取子分类
     * @param $parent_cate_id
     */
    public function getSubCategoryByPid($parent_cate_id){
        $where = [
            ["parent_cate_id","=",$parent_cate_id],
            ["flag","=",1]
        ];
        $fileds = ["id","category_name"];
        $data = SecQualifyCategory::where($where)->select($fileds)->orderBy("show_order","asc")->get();
        foreach ($data as &$item){
//            $levels = $item->levels;
//            $level_array = [];

            $item["levels"] = $this->getLevelsByCateIdFromQuota($item["id"]);
//            foreach ($levels as $level){
//                $level_item = [];
//                $level_item["level_id"] = $level["id"];
//                $level_item["level_name"] = $level["name"];
//                array_push($level_array,$level_item);
//
//            }
//            unset($item['levels']);
//            $item["levels"] = $level_array;

        }
        return $data;


    }
    //通过id 在quota表中获取资质分类的所有级别
    protected function getLevelsByCateIdFromQuota($cateId){
        $where = [
            ["subcategory_id","=",$cateId],
            ["flag","=",1]
        ];
        $field = ["level"];
        $data = Quota::where($where)->select($field)->orderBy("level","asc")->get();
        if($data == null){
            return null;
        }
        $levels = [];
        foreach ($data as $item){
            $tmp = [];
            $tmp["level_id"] = $item['level'];
            $tmp["level_name"] = $this->getLevelNameById($item['level']);
//            switch ($item['level']){
//                case 1: $tmp["level_id"] = $item['level']; $tmp["level_name"] = "一级" ;break;
//                case 2: $tmp["level_id"] = $item['level']; $tmp["level_name"] = "二级" ;break;
//                case 3: $tmp["level_id"] = $item['level']; $tmp["level_name"] = "三级" ;break;
//                case 4: $tmp["level_id"] = $item['level']; $tmp["level_name"] = "A级" ;break;
//                case 5: $tmp["level_id"] = $item['level']; $tmp["level_name"] = "B级" ;break;
//                case 6: $tmp["level_id"] = $item['level']; $tmp["level_name"] = "C级" ;break;
//                case 7: $tmp["level_id"] = $item['level']; $tmp["level_name"] = "甲级" ;break;
//                case 8: $tmp["level_id"] = $item['level']; $tmp["level_name"] = "甲级" ;break;
//                case 9: $tmp["level_id"] = $item['level']; $tmp["level_name"] = "甲级" ;break;
//                case 10: $tmp["level_id"] = $item['level']; $tmp["level_name"] = "无等级" ;break;
//                default:$tmp["level_id"] = $item['level']; $tmp["level_name"] = "无等级" ;break;
//
//            }
            array_push($levels,$tmp);
        }
        return $levels;

    }
    public function getFullCategoryNameByID($category_id){
        $where = [
            ["id","=",$category_id],
            ["flag","=",1]
        ];
        $fileds = ["id","category_name","parent_cate_id"];
        $data = SecQualifyCategory::where($where)->select($fileds)->first();
        if(!$data){
            return "";
        }
        $data = $data->toArray();
        $subcatename = $data['category_name'];
        $where_sup = [
            ["id","=",$data['parent_cate_id']],
            ["flag","=",1]
        ];
        $supcatename = SupQualifyCategory::where($where_sup)->select(["category_name"])->first();
        if($supcatename){
            $supcatename = $supcatename->toArray()["category_name"];
        }

        return $supcatename."-".$subcatename;



    }
    public function getCategoryDetailByID($id,$page,$tag){
        $pageSize = 10;
        $where = [
            ["flag","=",1]
        ];
        $field = ["id","name","address","logo","star","view_num"];
        $data = Company::where($where)->select($field)->orderBy('show_order', 'asc')->paginate($pageSize);
        $total = $data->total();
        $pageNum =  ceil($total/$pageSize);

        $companyList = [];
        foreach ($data as &$item){
            $item["logo"] = config('admin.upload.host').$item['logo'];
            $item["logo_watermark"] = "http://img.qikuai100.cn/logoS.png";
            $subcateroys = $item->subcategorys;
            $category = " ";
            if($subcateroys != null){
                foreach ($subcateroys as &$cate){
                    $category .= $cate["category_name"]."-";
                }
                $category = substr($category,1,strlen($category)-2);
            }

            $item["categorys"] = $category;
            unset($item['subcategorys']);
            $companyList[] = $item;

        }
        $ret = [
            "totalNum" =>$total,
            "totalPage"=>$pageNum,
            "currentPage"=>$data->currentPage(),
            "list"=>$companyList
        ];

        return $ret;
    }
    public function getLevelNameById($id){
        $where = [
            ['id','=',$id]
        ];
        $name = "";
        $fields = ["name"];
        $level = Level::where($where)->select($fields)->first();
        if($level){
            return $level["name"];
        }
        return $name;

    }

}